package com.cheboohui.iqs.taiping.direct.order;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.cheboohui.iqs.common.basic.TimeUtil;
import com.cheboohui.iqs.config.Config;
import com.cheboohui.iqs.spring.entity.Policy;
import com.cheboohui.iqs.spring.entity.price.request.PayRequest;
import com.cheboohui.iqs.spring.entity.price.response.ConfirmInsuredResult;
import com.cheboohui.iqs.sql.entity.Pay;
import com.cheboohui.iqs.taiping.direct.PublicNew;
import com.cheboohui.iqs.taiping.direct.ResTaiPing;
import com.cheboohui.iqs.taiping.direct.v.V04Request;

/**
 * 支付登记
 * 
 * @author xielonghai
 *
 */
public class PayPolicy {
	/**
	 * logger:日志工具.
	 */
	private static Log log = LogFactory.getLog(PayPolicy.class);

	/**
	 * 支付登记数据存入数据
	 * 
	 * @param payRequest
	 * @return
	 */
	public static Pay getDB(PayRequest payRequest) {
		Pay pay = new Pay();
		Policy p = payRequest.getPolicy();

		pay.setSource("TAIPING");
		pay.setCompany_code("TAIPING");
		pay.setGrade_time(TimeUtil.timeFormat(new Date(), TimeUtil.DEFAULT_TIME));
		pay.setPay_process("paybegin");
		pay.setOut_trade_no(p.getOrderNo());// 订单号
		pay.setOreder_subject("太平直连支付");
		pay.setTotal_amount("" + p.getTotal());// 订单金额
		pay.setBody("太平直连---支付登记");
		pay.setTimeout_express("120");
		pay.setShow_url(payRequest.getPay().getContracBackUrl());// 支付后承保回调地址
		pay.setSuccess_back_url(payRequest.getPay().getPayFrontBackUrl());// 前端地址
		pay.setFail_back_url(payRequest.getPay().getPayFrontBackUrl());// 前端地址
		pay.setProduct_code("TP-DIRECT");
		pay.setFront_notice(payRequest.getPay().getPayBackUrl());// 支付回调地址
		return pay;
	}

	/**
	 * 车险支付登记
	 * 
	 * @param payRequest
	 * @return
	 */
	public static ConfirmInsuredResult pay(PayRequest payRequest) {
		log.info(Config.value + "--车险支付登记实现接口....");
		log.info(Config.value + "--车险支付登记实现接口...支付成功后回调业管的地址:" + payRequest.getPay().getPayBackUrl());
		log.info(Config.value + "--车险支付登记实现接口...承保成功后回调业管的地址:" + payRequest.getPay().getContracBackUrl());
		log.info(Config.value + "--车险支付登记实现接口...支付成功后前端页面回调业管的地址:" + payRequest.getPay().getPayFrontBackUrl());

		ResTaiPing v04 = V04Request.requestV04("", getParamten(payRequest));
		if (!v04.isState()) {
			return new ConfirmInsuredResult(v04.getError(), v04.getMsg());
		}

		payRequest.getPolicy().setCode("07");
		payRequest.getPolicy().setCodeValue("待支付");
		payRequest.getPolicy().setPayUrl(v04.getResult().getJSONArray("basePay").getJSONObject(0).getString("payUrl"));

		log.info(Config.value + "--车险支付登记实现接口....请求v04以及处理结束,支付URL:" + payRequest.getPolicy().getPayUrl());
		return new ConfirmInsuredResult(payRequest.getPolicy());
	}

	/**
	 * V04接口请求参数转化
	 * 
	 * @param payRequest
	 * @return
	 */
	private static JSONObject getParamten(PayRequest payRequest) {
		JSONObject param = new JSONObject();
		param.put("orderNo", payRequest.getPolicy().getOrderNo());// 业务单号
		param.put("requestSource", "wapPay2");// 支付请求来源
		param.put("partnerUrl", PublicNew.getRollBackUrl() + "front/back");// TP前端回调地址

		return param;
	}
}
